/**
 * 
 */
package bud.util;

import java.util.List;
import java.util.Vector;

/**
 * @author Zhangwq
 */
@SuppressWarnings("unchecked")
public class TDArray
{
    private List m_row = new Vector();
    private int m_colnum = 1;

    public TDArray()
    {
    }

    public TDArray(int length)
    {
        this.m_colnum = length;
    }

    public void add(Object obj)
    {
        List col = new Vector();
        if (!this.m_row.isEmpty())
        {
            col = (List) this.m_row.get(this.m_row.size() - 1);
        }
        if (col.size() == this.getColnum())
        {
            col = new Vector();
        }
        if (col.size() == 0)
        {
            this.m_row.add(col);
        }
        if (col.size() < this.getColnum())
        {
            col.add(obj);
        }
    }

    public Object get(int row, int col)
    {
        if (col > this.m_colnum)
        {
            return null;
        }
        return ((List) this.m_row.get(row-1)).get(col-1);
    }

    public void set(int row, int col, Object obj)
    {
        if (col > this.m_colnum)
        {
            return;
        }
        ((List) this.m_row.get(row-1)).set(col-1, obj);
    }

    public boolean isEmpty()
    {
        return this.getRownum() == 0;
    }
    public int getColnum()
    {
        return this.m_colnum;
    }

    public int getRownum()
    {
        return this.m_row.size();
    }
    
    public String toString()
    {
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<this.getRownum();i++)
        {
            for(int j=0; j<this.getColnum();j++)
            {
                sb.append(((List)m_row.get(i)).get(j));
            }
            sb.append("\n");
        }
        return sb.toString();
    }
}
